//
//  ChangeLoginPassController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/31.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "HandleNetLogin.h"
#import "UserModel.h"
#import "DBAcquireAuthCodeButton.h"
#import "ChangeLoginPassController.h"
#import "ValidateTools.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "CKPuslichTool.h"
#import "MyNavigationController.h"


@interface ChangeLoginPassController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPass;
@property (weak, nonatomic) IBOutlet UITextField *nowPass;
@property (weak, nonatomic) IBOutlet UITextField *nowPass2;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet DBAcquireAuthCodeButton *codeBt;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UILabel *reminder2;
@property (weak, nonatomic) IBOutlet UILabel *reminder3;
@property (weak, nonatomic) IBOutlet UILabel *reminder4;
@property (weak, nonatomic) IBOutlet UILabel *reminder;
@property (strong,nonatomic)NSDate *oldDate;
@property (weak, nonatomic) IBOutlet UIButton *submitBt;
@property (strong,nonatomic)NSString *mCode;
@end

@implementation ChangeLoginPassController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.reminder.text=@"";
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder3.text=@"";
    self.reminder4.text=@"";
    [self.nowPass addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    [self setTextfile:self.oldPass img:[UIImage imageNamed:@"mima"]];
    [self setTextfile:self.nowPass img:[UIImage imageNamed:@"mima"]];
    [self setTextfile:self.nowPass2 img:[UIImage imageNamed:@"mima"]];
    
    [self MoveTextfileContent:self.codeTF];
    self.codeBt.layer.cornerRadius=5;
    self.submitBt.layer.cornerRadius=5;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
-(void)textFieldDidEndEditing:(UITextField *)mTextField{
    NSString *text=mTextField.text;
    if (text.length==0) {
        return;
    }
    self.reminder2.text=@"";
    BOOL onlyABC=[ValidateTools PasswordOnlyABC:text];
    BOOL onlyNo=[ValidateTools PasswordOnlyNo:text];
    BOOL onlyMark=[ValidateTools PasswordOnlyMark:text];
    if (onlyNo||onlyABC||onlyMark||text.length==0) {
        self.reminder2.text=@"*<低>有被盗风险,建议使用字母、数字、符号两种及以上组合";
    }else{
        BOOL tow1=[ValidateTools validatePassword:text];
        BOOL tow2=[ValidateTools validatePassword2:text];
        BOOL tow3=[ValidateTools validatePassword3:text];
        if (tow1||tow2||tow3) {
            self.reminder2.text=@"*<中>安全强度适中，可以使用三种的组合来提高安全强度";
        }else{
            self.reminder2.text=@"*<高>你的密码很安全";
        }
        
        
    }
    
}
-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(IBAction)tarStartButtonTimers:(id)sender{

    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder3.text=@"";
    //验证密码
    if (self.oldPass.text.length==0) {
        self.reminder1.text=@"*请输入原密码";
        return;
    }
    if (self.oldPass.text.length<6) {
        self.reminder1.text=@"*请输入6-20位密码";
        return;
    }
    if (self.nowPass.text.length==0) {
        self.reminder2.text=@"*请输入新密码";
        return;
    }
    if (self.nowPass.text.length<6) {
        self.reminder2.text=@"*请输入6-20位密码";
        return;
    }
    if (self.nowPass2.text.length==0) {
        self.reminder3.text=@"*请再次输入新密码";
        return;
    }
    if (![self.nowPass.text isEqualToString:self.nowPass2.text]) {
        self.reminder3.text=@"*两次密码输入不一致";
        return;
    }
    UserModel *model=[UserModel getUser];
    [HandleNetLogin getCodeForPhone:model.Phone Success:^(NSDictionary *data) {
        
        _mCode = data[@"Verificationcode"];;
        _oldDate=[NSDate date];
        [_codeBt tarStartButtonTimer];
    } Failure:^(NSString *errorInfo) {
        self.reminder.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }]; 
}
- (IBAction)backTop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)submit:(id)sender {
    self.reminder.text=@"";
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder3.text=@"";
    self.reminder4.text=@"";
    //验证密码
    //验证密码
    if (self.oldPass.text.length==0) {
        self.reminder1.text=@"*请输入原密码";
        return;
    }
    if (self.oldPass.text.length<6) {
        self.reminder1.text=@"*请输入6-20位密码";
        return;
    }
    if (self.nowPass.text.length==0) {
        self.reminder2.text=@"*请输入新密码";
        return;
    }
    if (self.nowPass.text.length<6) {
        self.reminder2.text=@"*请输入6-20位密码";
        return;
    }
    if (self.nowPass2.text.length==0) {
        self.reminder3.text=@"*请再次输入新密码";
        return;
    }
    if (![self.nowPass.text isEqualToString:self.nowPass2.text]) {
        self.reminder3.text=@"*两次密码输入不一致";
        return;
    }

    if (self.codeTF.text.length==0) {
        self.reminder4.text=@"*请输入手机验证码";
        return;
    }
    NSDate *currentDate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate *localDate1 = [currentDate dateByAddingTimeInterval:interval];
    NSDate *localDate2 = [_oldDate dateByAddingTimeInterval:interval];
    
    //计算时间差间隔
    NSTimeInterval timeBetween = [localDate1 timeIntervalSinceDate:localDate2];
    NSLog(@"timeBetween---%f",timeBetween);
    if (timeBetween>60*10) {
        self.reminder4.text=@"*验证码已失效";
        return;
    }
    UserModel *model=[UserModel getUser];
    [HandleNetLogin ChangePassWithPhone:model.Phone PassWord:self.oldPass.text NewPassWord:self.nowPass.text Success:^(NSDictionary *data) {
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USERISLOGINKEY"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //    NSDictionary *dic=@{@"index":@"0"};
        //    [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
//         [self.navigationController popViewControllerAnimated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        
        
        
        LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
        tempAppDelegate.window.rootViewController=nav;
        
       
    } Failure:^(NSString *errorInfo) {
        self.reminder.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }];
    
}

@end
